#include<stdio.h>
#include"tools.h"
#include"getch.h"
struct Teacher tea[10];
struct Student stu[100];
struct Admin admin;

//文件读
void Admin_file_read()
{
	FILE* frp=fopen("admin.txt","rb+");
	if(NULL==frp)
	{
		puts("文件打开失败!");	
		return;
	}
	if(fread(&admin,sizeof(struct Admin),1,frp)!=1)
	{

		puts("校长信息加载失败!");
	}
	fclose(frp);
	frp=NULL;

}

void Teacher_file_read()
{
	FILE* frp=fopen("teacher.txt","rb+");
	if(NULL==frp)
	{
		puts("文件打开失败!");
		return;
	}
	if(fread(&tea_n,sizeof(int),1 ,frp)!=1)
	{
		tea_n=0;
		puts("读取教师数量错误!");
	}
	else
	{
		for(int i=0;i<tea_n;i++)
		{
			fread(&tea[i],sizeof(struct Teacher),1,frp);
		}
	}
	fclose(frp);
	frp=NULL;

}

void Student_file_read()
{
	FILE* frp=fopen("student.txt","rb+");
	if(frp==NULL)
	{
		puts("打开文件失败!");
		return;
	}
	if(fread(&stu_n,sizeof(int),1,frp)!=1)
	{
		stu_n=0;
		puts("读取学生数量出错!");
		return;
	}
	else
	{
		for(int i=0;i<stu_n;i++)
		{
			fread(&stu[i],sizeof(struct Student),1,frp);
		}
	}
	fclose(frp);
	frp=NULL;
}

//文件写

void Admin_file_write()
{
	FILE* fwp=fopen("admin.txt","wb+");
	fwrite(&admin,sizeof(struct Admin),1,fwp);
	fclose(fwp);
	fwp=NULL;
}

void Teacher_file_write()
{
//	printf("...............\n");
	FILE* fwp=fopen("teacher.txt","wb+");
//	printf("%d\n",tea_n);
	if(fwrite(&tea_n,sizeof(int),1,fwp)!=1)
	{
		puts("写入错误!");
		return;
	}
	
//	printf("1111111111111111111\n");
	for(int i=0;i<tea_n;i++)
	{
		fwrite(&tea[i],sizeof(struct Teacher),1,fwp);
//		printf("-----\n");
	}
	fclose(fwp);
	fwp=NULL;
//	printf("-----------------\n");
}

void Student_file_write()
{
	FILE* fwp=fopen("student.txt","wb+");
	if(fwrite(&stu_n,sizeof(int),1,fwp)!=1)
	{
		puts("写入错误!");
		return;
	}
	for(int i=0;i<stu_n;i++)
	{
		fwrite(&stu[i],sizeof(struct Student),1,fwp);
	}
	fclose(fwp);
	fwp=NULL;

}
//密码输入保护
void get_pwd(char* pwd)
{
	
	for(int i=0;i<6;i++)
	{
		pwd[i]=getch();
		printf("*");
	}
	pwd[6]='\0';
}


